<?php
	use\ampleharvest\code\cRestConnector;
	use\ampleharvest\code\cRestServices;
	use\ampleharvest\code\cConfig;

	require_once dirname(dirname(dirname(__FILE__))) . "/classes/cThread.php";
	require_once dirname(dirname(dirname(__FILE__))) . '/classes/cStomp.php';
	require_once dirname(dirname(dirname(__FILE__))) . "/config/cRestServices.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/cRestConnector.php";
	require_once dirname(dirname(dirname(__FILE__))) . '/classes/cAmpleHarvestLog.php';

	$arr_Params = array(
		"username::",
		"id::"
	);

	$arr_Options = getopt("", $arr_Params);

	$obj_MarkDataAsRead = new cMarkDataAsRead($arr_Options['username'], $arr_Options['id']);

	class cMarkDataAsRead
	{
		private $m_str_UserName;
		private $m_str_ID;
		private $m_obj_Log;

		private $m_arr_Views;

		private static $m_obj_RestCon = null;
		private static $m_obj_STOMP = null;

		protected function get_RestCon()
		{
			if (!self::$m_obj_RestCon)
			{
				self::$m_obj_RestCon = new cRestConnector(cRestServices::get_Views($this->m_str_ID));
				self::$m_obj_RestCon->set_Login(cConfig::C_STR_AMPLE_SILO_USER_NAME, cConfig::C_STR_AMPLE_SILO_PASSWORD);
			}

			return self::$m_obj_RestCon;
		}

		protected function getCurrentViews()
		{
			$obj_RestCon = $this->get_RestCon();
			return $obj_RestCon->send_Get();
		}

		public function __construct($str_UserName, $str_ID)
		{
			$this->m_str_ID = $str_ID;
			$this->m_str_UserName = $str_UserName;
			$this->m_obj_Log = new \cAmpleHarvestLog();

			$str_Content = $this->getCurrentViews();
			if (!$str_Content)
			{
				$this->add_User2Read();
				return;
			}

			//$this->m_arr_Views = json_decode($str_Content);

			if (!$this->m_arr_Views)
			{
				$this->add_User2Read();
				return;
			}

			if (!isset($this->m_arr_Views->$str_UserName))
			{
				$this->add_User2Read();
				return;
			}
		}

		function add_User2Read()
		{
			if (!is_object($this->m_arr_Views))
			{
				$this->m_arr_Views = new \stdClass();
			}

			$str_UserName = $this->m_str_UserName;

			$this->m_arr_Views->$str_UserName = $str_UserName;

			$obj_RestCon = $this->get_RestCon();
			$obj_RestCon->set_Url(cRestServices::put_Views());

			$obj_RestCon->set_Param("viewContent", json_encode($this->m_arr_Views));
			$obj_RestCon->set_Param("id", $this->m_str_ID);

			$obj_RestCon->send_Put();
			$this->queue4Update();

		}

		protected function queue4Update()
		{
			$obj_STOMP = $this->get_STOMP();

			$obj_STOMP->send(\cStomp::get_SolrIndexQueue(), $this->m_str_ID, 1);

			$str_Command = dirname(dirname(__FILE__)) . "/tasks/task_runner.php --task=index_Data_Queue --optimize=0 ";
			exec(cConfig::C_STR_PATH_PHP . " " . $str_Command);
		}

		protected function log($str_Msg, $int_LogLevel = cAmpleHarvestLog::C_INT_LEVEL_INFO)
		{
			$this->m_obj_Log->do_Log(cAmpleHarvestLog::C_STR_ACTION_TASK_INFO,
				"Script - MarkDataAsRead - " . $str_Msg,
				$int_LogLevel);
		}

		protected function log_Error($str_Msg, $int_LogLevel = cAmpleHarvestLog::C_INT_LEVEL_ERRORS)
		{
			$this->m_obj_Log->do_Log(cAmpleHarvestLog::C_STR_ACTION_TASK_ERROR,
				"Script - MarkDataAsRead - " . $str_Msg,
				$int_LogLevel);
		}
		protected function get_STOMP()
		{
			if (!self::$m_obj_STOMP)
			{
				self::$m_obj_STOMP = new \cStomp();
			}

			return self::$m_obj_STOMP;
		}
	}

